function averages = gen_ca_fig6c

pool_length_in_bins = 40;

seed = 0;
averages = single_subject(seed)
i=2;
for seed = 100:100:400
    averages(:,:,i) = single_subject(seed);
    i=i+1;
end
pooled_averages = mean(averages,3);

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(pooled_averages(2,:),'g','LineWidth',3);
plot(pooled_averages(3,:),'r','LineWidth',3);
plot(pooled_averages(4,:),'c','LineWidth',3);

ylabel('Match score','FontSize',16,'Color','w')
xlabel('T (sec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/4:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'3','3.5','4','4.5','5'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stimulus B at theta deg.','Stim. B at theta + 90 deg.','Stim. B at theta + 180 deg.','Stim. B at theta + 270 deg.');
set(h,'Color','w');




end

% Read data from the pwd.
function subject1_pooled_averages = single_subject(seed)

filename = dir(['final_seed' num2str(seed,'%03d') '_phase_6*']);
cd (filename.name);



analyzeSequenceEssential('P','p6(L4)',500,999,810001,817999,905000,1224999)
global PSTATES

% 64, 5-second trials.

pool_length_in_bins = 40;

% Reconstruct the start bins for the Mental rotation experiment: 64 trials
% of 5 seconds starting at the response period, 3 seconds into the trial.
startbin=3*20+1;trial_bins = reshape(startbin:100:64*100,8,8)';

stimulus_numbers = repmat([1:4],4,1);
object1_pool_start_bins = [];
object1_object_to_pool = [];
for position = 1:4
    temp = trial_bins(1:4,1:4);
    object1_pool_start_bins(position,:) = temp(:)';

    object1_object_to_pool(position,:) = stimulus_numbers(:)';
    stimulus_numbers = [stimulus_numbers(:,2:end) stimulus_numbers(:,1)];
    
end

object1_pooled_averages = plot_georgopoulos(PSTATES, object1_pool_start_bins,...
    object1_object_to_pool , pool_length_in_bins);


% Analyze the other object now; template is taken from different window
% containing object2.
analyzeSequenceEssential('P','p6(L4)',500,999,856000,863999,905000,1224999)
global PSTATES


stimulus_numbers = repmat([1:4],4,1);
object2_pool_start_bins = [];
object2_object_to_pool = [];
for position = 1:4
    temp = trial_bins(5:8,5:8);
    object2_pool_start_bins(position,:) = temp(:)';

    object2_object_to_pool(position,:) = stimulus_numbers(:)';
    stimulus_numbers = [stimulus_numbers(:,2:end) stimulus_numbers(:,1)];
    
end
object2_pooled_averages = plot_georgopoulos(PSTATES, object2_pool_start_bins,...
    object2_object_to_pool , pool_length_in_bins);


subject1_pooled_averages = (object1_pooled_averages + object2_pooled_averages)*0.5;

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(subject1_pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(subject1_pooled_averages(2,:),'g','LineWidth',3);
plot(subject1_pooled_averages(3,:),'r','LineWidth',3);
plot(subject1_pooled_averages(4,:),'c','LineWidth',3);

ylabel('Match strength','FontSize',16,'Color','w')
xlabel('T (msec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/4:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'0','500','1000','1500','2000'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stimulus B angle','Stim. B angle + 90 deg.','Stim. B angle + 180 deg.','Stim. B angle + 270 deg.');
set(h,'Color','w');

cd ..


end

